package cn.phub.org.controller;


import cn.phub.basic.util.AjaxResult;
import cn.phub.basic.util.PageList;
import cn.phub.org.domain.Employee;
import cn.phub.org.query.EmployeeQuery;
import cn.phub.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理接口
 *  写接口的三要素：
 *    1.请求地址是什么
 *    2.请求的参数是什么
 *    3.返回值是什么
 *
 *
 *
 */
@RestController
//@GetMapping = @RequestMapping(value="/id", method = RequestMethod.GET)
//@PutMapping
//@PostMapping
//@PatchMapping
@RequestMapping("/employee")  //http://localhost:8080/dept
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    // 新增或修改员工   put
    @PutMapping   //http://localhost:8080/dept
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() != null){
                employeeService.update(employee);
            }else{
                employeeService.add(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！");
        }
    }

    //删除一条记录  delete  http://locahost:8080/dept/1
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        // ctrl + alt + t
        /**
         * 高技玩法
         */
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！");
        }
        /*
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult result = new AjaxResult();
            result.setSuccess(Boolean.FALSE);
            result.setMessage("系统繁忙，稍后重试！！！");
            return result;
        }*/
    }

    /**
     * 如果前端传递的是 数组
     * 那么 后端可以使用数组或者list
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){ //前端给了一个JSON的数组,这里用list或array接收都可以
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！");
        }
    }


    //查询一条记录  get  http://localhost:8080/dept/1
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee queryById(@PathVariable("id") Long id){
        return  employeeService.queryById(id);
    }


    //查询所有记录  get    //http://localhost:8080/dept
    @GetMapping
    public List<Employee> queryAll(){
        return  employeeService.queryAll();
    }


    //分页查询  + 高级查询 post http://localhost:8080/dept?username=zs&age=18
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return  employeeService.queryPage(query);
    }


}
